package com.ruoyi.web.controller.common.service.Impl;

import com.ruoyi.common.entity.DO.CommonImageDO;
import com.ruoyi.common.entity.DO.FileDO;
import com.ruoyi.common.entity.VO.CommonImageVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.controller.common.mapper.IBackCommonImageMapper;
import com.ruoyi.web.controller.common.service.IBackCommonImageService;
import com.ruoyi.web.controller.upload.service.IUploadService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IBackCommonImageServiceImpl implements IBackCommonImageService {

    @Resource
    private IBackCommonImageMapper commonImageMapper;

    @Resource
    private IUploadService uploadService;

    @Override
    public List<CommonImageDO> searchList(CommonImageDO commonImageDO) {
        commonImageDO.setCoverUrl(null);
        return commonImageMapper.select(commonImageDO);
    }

    @Override
    public CommonImageDO getImageInfoById(Long id) {
        return commonImageMapper.selectByPrimaryKey(id);
    }

    @Override
    public String add(CommonImageVO commonImageVO) {
        if (StringUtils.isNotEmpty(commonImageVO.getImageUrls())) {
            String[] imageUrlArr = commonImageVO.getImageUrls().split(",");
            for (String imageUrl : imageUrlArr
            ) {
                CommonImageDO commonImageDO = new CommonImageDO();
                BeanUtils.copyProperties(commonImageVO, commonImageDO);
                commonImageDO.setCoverUrl(imageUrl);
                commonImageMapper.insertSelective(commonImageDO);
            }
            return "ok";
        } else if (StringUtils.isNotEmpty(commonImageVO.getCoverUrl())) {
            CommonImageDO commonImageDO = new CommonImageDO();
            BeanUtils.copyProperties(commonImageVO, commonImageDO);
            commonImageMapper.insertSelective(commonImageDO);
            return commonImageVO.getCoverUrl();
        } else if (commonImageVO.getCoverFile()!=null) {
            FileDO fileDO = uploadService.addImageFile( commonImageVO.getCoverFile(),0,commonImageVO.getCreateBy(),0L);
            CommonImageDO commonImageDO = new CommonImageDO();
            commonImageVO.setCoverUrl(fileDO.getUrl());
            BeanUtils.copyProperties(commonImageVO, commonImageDO);
            commonImageMapper.insertSelective(commonImageDO);
            return commonImageDO.getCoverUrl();
        } else {
            throw new BusinessException("请至少上传1张图片！");
        }

    }

    @Override
    public void edit(CommonImageDO commonImageDO) {
        CommonImageDO commonImageInfo = commonImageMapper.selectByPrimaryKey(commonImageDO.getId());
        if (commonImageInfo == null || commonImageInfo.getIsDelete() == 1) {
            throw new BusinessException("图片已被删除!");
        }
        BeanUtils.copyProperties(commonImageDO, commonImageInfo);
        commonImageMapper.updateByPrimaryKeySelective(commonImageDO);
    }

    @Override
    public int remove(String ids) {
        return commonImageMapper.deleteByIds(ids);
    }
}
